﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace VendorContacts
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Task task = null;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnChooseFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog ofd = new Microsoft.Win32.SaveFileDialog();

            ofd.DefaultExt = "xml";
            ofd.Filter = "*.xml|xml";

            bool? ret = ofd.ShowDialog();

            if (ret == null || ret.Value != true)
            {
                return;
            }
            String file = ofd.FileName;
            this.task = Task.Factory.StartNew(new Action(() => this.TaskFun(file)));
        }

        private void TaskFun(String file)
        {
            try
            {
                this.Dispatcher.BeginInvoke(new Action(() => this.btnChooseFile.IsEnabled = false));
                XDocument xDoc = XDocument.Parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Contacts/>");
                VendorService vendorService = ServiceContainer.GetService<VendorService>();
                Vendor[] vendors = vendorService.GetByAll("", "", "",VendorType.None, 0, 0).Datas.ToArray();
                int count = 0;
                foreach (Vendor ven in vendors)
                {
                    XElement xe = new XElement("Contact");
                    //厂家名称
                    xe.Add(new XElement("Name", ven.Name));

                    //联系方式
                    XElement xPhonseList = new XElement("PhoneList");
                    String[] strPhonse = ven.Phone.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var pho in strPhonse)
                    {
                        xPhonseList.Add(new XElement("Phone", pho, new XAttribute("Type", "2")));
                    }
                    xe.Add(xPhonseList);

                    //地址
                    xe.Add(new XElement("PostalAddressList", new XElement("PostalAddress", ven.MarketAddress, new XAttribute("Type", "2"))));

                    //网址
                    xe.Add(new XElement("OrganizationList", new XElement("Organization", new XAttribute("Type", "1"), ven.HomePage, new XElement("Detail", new XElement("Company", ven.HomePage)))));

                    //QQ
                    XElement xQQ = new XElement("IMList");
                    string[] strQQs = ven.QQ.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string strQQ in strQQs)
                    {
                        xQQ.Add(new XElement("IM", strQQ, new XAttribute("Type", "pre:4")));
                    }
                    xe.Add(xQQ);

                    //所属组
                    xe.Add(new XElement("GroupList", new XElement("GroupName", "商贸城厂家")));

                    this.Dispatcher.Invoke(new Action(() => this.tbMessage.AppendText(DateTime.Now + " :  " + (++count + " / " + vendors.Length) + " " + ven.Name + Environment.NewLine)));
                    this.Dispatcher.Invoke(new Action(() => this.tbMessage.ScrollToEnd()));
                    xDoc.Root.Add(xe);
                }
                xDoc.Save(file);
                MessageBox.Show("已完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Dispatcher.BeginInvoke(new Action(() => this.btnChooseFile.IsEnabled = true));
            }
        }
    }
}
